<?php
namespace app\components;

use Yii;
use yii\web\Controller;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AdminController
 *
 * @author Administrator
 */

class AdminController extends Controller{
    //put your code here
    public $layout = "@app/views/layouts/admin";
    public $admin_menu;
    
    
    public function init(){		
        //检测权限
        $userid = Yii::$app->user->identity->id;
//        print_r(Yii::$app->requestedRoute);exit;
        $source = \common\models\SysUser2role::getUserSource($userid);
//        print_r($source);exit;
//        var_dump(array_search(Yii::$app->requestedRoute, $source)) ;exit;
        $isAllow = false;
        $admin_menu=[];
        foreach ($source as $value){
            $selected = 0;
            if(Yii::$app->requestedRoute == $value['url']){
                $selected = 1;
            }
            if($value['parent']==0 && $value['ismenu']==1){
                $admin_menu[$value['id']]['main'] = ['name'=>$value['name'],'url'=>$value['url'],'ismenu'=>$value['ismenu'],'selected'=>$selected];
                $admin_menu[$value['id']]['sub'] = [];
            }elseif($value['parent']!=0){
                if($selected == 1){
                    $admin_menu[$value['parent']]['main']['selected'] = $selected;
                }
                array_push($admin_menu[$value['parent']]['sub'], ['name'=>$value['name'],'url'=>$value['url'],'ismenu'=>$value['ismenu'],'selected'=>$selected]);
            }
            
            if(array_search(Yii::$app->requestedRoute, $value)){
                $isAllow = true;
            }
        }
        if($isAllow  == false){
            \common\models\Tools::get_show_msg('/', '没有权限', 0, 3);
        }
        
        $this->admin_menu = $admin_menu;
		// print_r($admin_menu);
		// exit;
        Yii::$app->view->params['admin_menu'] = $admin_menu;
    }
}
